string_hash
This function generates what is known as a hash for a block of data, using one of several available algorithms.
string string_hash(string data, int algorithm, bool binary)
Parameters:
data
The data from which the hash should be generated.
algorithm
A number indicating which hashing algorithm to use (see remarks).
binary
A boolean specifying whether or not the resulting string should be in binary or hex form, true meaning binary.
Return value:
The generated hash on success, or a blank string on failure.
Remarks:
A hash is a fixed size representation of an arbitrary amount of data. What this means in practice is that you can pass a block of data of any length to this function and you will get a string back which is a sort of identifier of the data, always with the same size. It doesn't matter if the data block is 1 kb or 1 gb, the size of the hash will always be the same. The hash will change completely if even one byte of the data block is different, making it very useful for tasks such as integrity verification of files etc.
The available algorithms are:
Please note that due to the nature of hashes, some completely different blocks of data will ultimately generate the same hash. This is an inevitable consequence of trying to convert an arbitrary amount of data to a fixed size representation, since a hash is entirely different from data compression. However, the frequency at which duplication occurs depends on what hashing algorithm you use. The 256 bit algorithms are more likely to generate duplicates than their 512 bit counterparts, which are twice as large and thus have more room for variation.
Example:
// Generate a SHA256 bit hash for a normal piece of text, as a hex string.
void main()
{
string text="Hello, this is the text from which the hash should be generated.";
alert("Original text", text);
text=string_hash(text, 1, false);
alert("Hash", text);
}